package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorMapper flavorMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品和口味
     *
     * @param dishDTO
     */
    @Override
    public void addDishWithFlavor(DishDTO dishDTO) {
        log.info("新增菜品和口味{}", dishDTO);
        //新增菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //插入菜品数据
        dishMapper.insert(dish);
        //获取新增菜品主键
        Long id = dish.getId();

        //新增菜品口味
        //判断用户是否选择了口味
        if (dishDTO.getFlavors() != null && dishDTO.getFlavors().size() != 0) {
            //获取菜品的多个口味
            List<DishFlavor> flavors = dishDTO.getFlavors();
            if (flavors != null && flavors.size() != 0) {
                //每个口味设置菜品id
                flavors.forEach(flavor -> flavor.setDishId(id));
            }
            //插入菜品口味数据
            flavorMapper.insert(flavors);
        }


    }

    /**
     * 分页查询菜品
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectDishPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //菜品是否为起售状态
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //菜品是否关联套餐
        for (Long id : ids) {
            //查询套餐下是否有菜品
            List<Long> setmealIds = setmealMapper.getSetmealIdsByDishId(id);
            if (setmealIds != null && setmealIds.size() != 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        //批量删除菜品和对应口味
        dishMapper.deleteByIds(ids);
        flavorMapper.deleteFlavorByDishIds(ids);
    }

    /**
     * 根据id查询菜品和口味信息
     *
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //菜品信息
        Dish dish = dishMapper.selectById(id);

        //菜品口味信息
        List<DishFlavor> flavors = flavorMapper.selectByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品
        dishMapper.updateDish(dish);

        //修改菜品口味
        //判断用户是否选择了口味
        if (dishDTO.getFlavors() != null && dishDTO.getFlavors().size() != 0) {
            //删除原始所有口味
            List<Long> ids = Arrays.asList(dish.getId());
            flavorMapper.deleteFlavorByDishIds(ids);
            //插入最新口味
            List<DishFlavor> flavors = dishDTO.getFlavors();
            flavors.forEach(flavor -> flavor.setDishId(dishDTO.getId()));
            flavorMapper.insert(flavors);
        }
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        //修改菜品状态
        dishMapper.updateDish(dish);

        //TODO
        //修改套餐状态
        //如果菜品为停售状态同时套餐也要为停售状态
        if (status == StatusConstant.DISABLE) {
            List<Long> setmealIds = setmealMapper.getSetmealIdsByDishId(id);
            //判断该菜品下是否有套餐,如果有套餐就修改套餐状态
            if (setmealIds != null && setmealIds.size() != 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(setmealId);
                    setmeal.setStatus(status);
                    log.info("setmeal:====================>{}",setmeal);
                    //修改每一个菜品关联的套餐状态
                    setmealMapper.update(setmeal);
                }
            }
        }
        //批量修改菜品关联的套餐状态
    }


    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> list = setmealMapper.selectAllByCategoryId(categoryId);
        return list;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
